﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SQLEditor.admin.entity;
using SQLEditor.admin.service;
using SQLEditor.dal;
namespace SQLEditor.admin.ashx
{
    /// <summary>
    /// rm_button 的摘要说明
    /// </summary>
    public class rm_button : rm_base
    {
        ButtonService bs = new ButtonService();
        public override void getall(HttpContext context)
        {
           string json =  bs.getall();
           context.Response.Write(json);
        }

        public override void search(HttpContext context)
        {
            string strWhere = "1=1";
            string sort = context.Request.Params["sort"] ?? "id";   //排序列
            string order = context.Request.Params["order"] ?? "asc";     //排序方式 asc或者desc
            int pageIndex = int.Parse(context.Request.Params["page"] ?? "1");
            int pageSize = int.Parse(context.Request.Params["rows"]?? "20");

            string json = bs.search(pageSize, pageIndex, sort + " " + order, strWhere);
            context.Response.Write(json);
                        
        }

        public override void add(HttpContext context)
        {
            string name = context.Request.Params["ui_button_name"] ?? "";
            string code = context.Request.Params["ui_button_code"] ?? "";
            string icon = context.Request.Params["ui_button_icon"] ?? "";
            string desc = context.Request.Params["ui_button_description"] ?? "";
            int sort = Convert.ToInt32(context.Request.Params["ui_button_sort"]);
            tbButton b = new tbButton {
                name = name,
                code = code,
                icon = icon,
                description = desc,
                sort = sort,
                addDate = DateTime.Now
            };
            string json = bs.add(b);
            context.Response.Write(json);
                        
        }
        public override void edit(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request.Params["ui_button_id"] ?? "0");
            string name = context.Request.Params["ui_button_name"] ?? "";
            string code = context.Request.Params["ui_button_code"] ?? "";
            string icon = context.Request.Params["ui_button_icon"] ?? "";
            string desc = context.Request.Params["ui_button_description"] ?? "";
            int sort = Convert.ToInt32(context.Request.Params["ui_button_sort"]);
            tbButton b = new tbButton
            {
                id = id,
                name = name,
                code = code,
                icon = icon,
                description = desc,
                sort = sort,
                addDate = DateTime.Now
            };
            string json = bs.edit(b);
            context.Response.Write(json);
        }

        public override void delete(HttpContext context) {
            string ids = context.Request.Params["ids"];
            string json = bs.delete(ids);
            context.Response.Write(json);
        }

        public override void other(HttpContext context)
        {
            string action = context.Request.Params["action"];
            switch (action)
            {
                case "getbutton":
                    {
                        UserEntity user = UserService.GetUser(context);
                        string pageName = context.Request.Params["pagename"];
                        string menuCode = context.Request.Params["menucode"];
                        string json = bs.getbutton(user.id, menuCode, pageName);
                        context.Response.Write(json);
                        break;
                    }
            }
        }
    }
}